<?php

class ChecklistPelaporanController extends Controller
{
    public $layout='//layouts/column2';

    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'izin_id',
		'visible' => false,
		'value' => '($data->Izin) ? $data->Izin->id : "-"',
	),array(
		'name' => 'pelaporan_id',
		'visible' => false,
		'value' => '($data->Pelaporan) ? $data->Pelaporan->nama : "-"',
	),array(
		'name' => 'status_kelengkapan',
		'visible' => false,
		'value' => '($data->status_kelengkapan) ? $data->status_kelengkapan : "-"',
	),array(
		'name' => 'upload_dokumen',
		'visible' => false,
		'value' => '($data->upload_dokumen) ? $data->upload_dokumen : "-"',
	),            array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn'
            ),
        );
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
	'name' => 'pelaporan_id',
	'value' => ($model->pelaporan) ? $model->pelaporan->nama : "-",
),array(
	'name' => 'status_kelengkapan',
	'value' => ($model->status_kelengkapan) ? $model->status_kelengkapan : "-",
),array(
	'name' => 'upload_dokumen',
        'type'=>'raw',
	'value' => ($model->upload_dokumen) ? CHtml::link($model->upload_dokumen, array('checklistPelaporan/download/'.$model->id),array('target'=>'_blank')) : "-",
));
        return $viewList;
    }
    
    public function actionDownload($id) {
        $model = $this->loadModel($id);
        $path = YiiBase::getPathOfAlias('webroot').$model->path_dokumen.'/';
        clearstatcache();
        ob_end_clean();
        if (!empty($model->upload_dokumen) && file_exists($path.$model->upload_dokumen)) {
            $file = $path.$model->upload_dokumen;
            header('Pragma: public');
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
            header('Cache-Control: no-store, no-cache, must-revalidate');
            header('Cache-Control: pre-check=0, post-check=0, max-age=0');
            header ("Pragma: no-cache");
            header("Expires: 0");
            header('Content-Transfer-Encoding: none');
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Type: application/pdf');
            header('Content-Length: ' . filesize($file));
            header('Content-Disposition: attachment; filename="'.basename($model->upload_dokumen).'"');
            readfile($file);
            Yii::app()->end();
            return;
        } else {
            echo 'Berkas tidak tersedia';
        }
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionDetail($id)
    {
        $model=$this->loadModel($id);
        $data = Izin::model()->findByPk($model->izin_id);
        $this->render('detail', array(
            'model' => $model,
            'data'=>$data,
        ));
    }
    
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['ChecklistPelaporan']))
        {
            $model->attributes=$_POST['ChecklistPelaporan'];
            $izinID = $model->izin_id;
            $data = Izin::model()->with('perusahaan')->findByPk($izinID);
                $badanUsaha = $data->perusahaan->bentuk_badan_usaha;
                $nama = $data->perusahaan->nama_perusahaan;
                $perusahaanId = $data->perusahaan_id;
                $namaAdmin = $model->administrasi->nama;
                
                if($data->perusahaan->jenis_usaha == 'Pengolahan MB')
                    $dirName = '1 Pengolahan MB';
                if($data->perusahaan->jenis_usaha == 'Pengolahan GB')
                    $dirName = '2 Pengolahan GB';
                if($data->perusahaan->jenis_usaha == 'Pengolahan HO')
                    $dirName = '3 Pengolahan HO';
                $num = $model->administrasi->id;
                $fileName = $num.'-'.$badanUsaha.' '.$nama.'-'.$namaAdmin.'.pdf';
            $file = CUploadedFile::getInstance($model, 'upload_dokumen');
            if (is_object($file) && get_class($file) === 'CUploadedFile')
                $model->upload_dokumen = $file;
            if ($model->validate()) {
                $rootDir = '/uploaded_files/attachment/'.$dirName.'/'.$perusahaanId.' '.$badanUsaha.' '.$nama;
                $path = YiiBase::getPathOfAlias('webroot').$rootDir;
                if (is_object($model->upload_dokumen)) {
                    $model->upload_dokumen->saveAs($path.'/7 Pelaporan/'.$fileName);
                }
                $model->upload_dokumen = (!empty($model->upload_dokumen)) ? $fileName : '';
                $model->status_kelengkapan = (!empty($model->upload_dokumen)) ? 'Ada' : 'Tidak';
                $model->path_dokumen = (!empty($model->upload_dokumen)) ? $rootDir : '';
                if($model->save())
                    $this->redirect(array('izin/'.$izinID));
            }
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['ChecklistPelaporan']))
        {
            $model->attributes=$_POST['ChecklistPelaporan'];
            $id = $model->izin_id;
            $perusahaan = Perusahaan::model()->findByPk($id);
                $badanUsaha = $perusahaan->bentuk_badan_usaha;
                $nama = $perusahaan->nama_perusahaan;
                $namaAdmin = $model->pelaporan->nama;
                
                if($perusahaan->jenis_usaha == 'Pengolahan MB')
                    $dirName = '1 Pengolahan MB';
                if($perusahaan->jenis_usaha == 'Pengolahan GB')
                    $dirName = '2 Pengolahan GB';
                if($perusahaan->jenis_usaha == 'Pengolahan HO')
                    $dirName = '3 Pengolahan HO';
                $num = $model->pelaporan->id;
                $fileName = $num.'-'.$badanUsaha.' '.$nama.'-'.$namaAdmin.'.pdf';
            $file = CUploadedFile::getInstance($model, 'upload_dokumen');
            if (is_object($file) && get_class($file) === 'CUploadedFile')
                $model->upload_dokumen = $file;
            if ($model->validate()) {
                $path = YiiBase::getPathOfAlias('webroot').'/uploaded_files/attachment/'.$dirName.'/'.$id.' '.$badanUsaha.' '.$nama;
                if (is_object($model->upload_dokumen)) {
                    $model->upload_dokumen->saveAs($path.'/1 Persyaratan Administratif/'.$fileName);
                }
                
                $model->upload_dokumen = $fileName;
                $model->status_kelengkapan = 'Ada';
                if($model->save())
                    $this->redirect(array('perusahaan/'.$id));
            }
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $model=new ChecklistPelaporan('publik');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['ChecklistPelaporan']))
            $model->attributes=$_GET['ChecklistPelaporan'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new ChecklistPelaporan('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['ChecklistPelaporan']))
            $model->attributes=$_GET['ChecklistPelaporan'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=ChecklistPelaporan::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='checklist-pelaporan-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
